//
//  UIViewController+DFExtension.m
//  Pods
//
//  Created by rujia chen on 17/4/13.
//
//

#import "UIViewController+DFExtension.h"
#import <Aspects/Aspects.h>
#import "NSObject+DFLayoutSupport.h"
#import "NSObject+DFSwizzle.h"
#import <objc/runtime.h>

@implementation UIViewController (DFExtension)

+ (void)load {
    [self aspect_hookSelector:@selector(viewWillAppear:) withOptions:AspectPositionBefore usingBlock:^(id<AspectInfo> info){
        UIViewController *controller = [info instance];
        BOOL animated = [[[info arguments] firstObject] boolValue];
        if ([controller.parentViewController isKindOfClass:[UINavigationController class]]) {
            [controller.navigationController setNavigationBarHidden:controller.df_shouldHideNavigationBar animated:animated];
            [controller setNavBarAppearance:controller.navigationController.navigationBar];
            [controller.navigationController setToolbarHidden:YES animated:animated];
        }
    } error:nil];
}

- (void)df_setShouldHideNavigationBar:(BOOL)df_shouldHideNavigationBar {
    objc_setAssociatedObject(self, @selector(df_shouldHideNavigationBar), @(df_shouldHideNavigationBar), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (BOOL)df_shouldHideNavigationBar {
    return [objc_getAssociatedObject(self, _cmd) boolValue];
}

- (void)df_setNavigationBarTintColor:(UIColor *)df_navigationBarTintColor {
    objc_setAssociatedObject(self, @selector(df_navigationBarTintColor), df_navigationBarTintColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIColor *)df_navigationBarTintColor {
    return objc_getAssociatedObject(self, _cmd);
}

- (void)df_setNavigationBarForegourndColor:(UIColor *)df_navigationBarForegourndColor {
    objc_setAssociatedObject(self, @selector(df_navigationBarForegourndColor), df_navigationBarForegourndColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIColor *)df_navigationBarForegourndColor {
    return objc_getAssociatedObject(self, _cmd);
}

- (void)df_setBackIndicatorImage:(UIImage *)df_backIndicatorImage {
    objc_setAssociatedObject(self, @selector(df_backIndicatorImage), df_backIndicatorImage, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIImage *)df_backIndicatorImage {
    return objc_getAssociatedObject(self, _cmd);
}

- (void)df_setTopLayoutGuideLength:(CGFloat)length {
    [self aspect_hookSelector:@selector(viewDidLoad) withOptions:AspectPositionAfter usingBlock:^(){
        id topLayoutGuide = self.topLayoutGuide;
        [topLayoutGuide df_setLayoutLength:length];
    } error:nil];   
}

- (void)df_setTopLayoutGuideLength:(CGFloat)height orientation:(UIInterfaceOrientation)orientation {
    
}

- (UIViewController *)df_topViewController {
    if (self.presentedViewController == nil) {
        return self;
    }else if ([self.presentedViewController isKindOfClass:[UINavigationController class]]) {
        UINavigationController *navigationController = (UINavigationController *)self.presentedViewController;
        UIViewController *lastViewController = [[navigationController viewControllers] lastObject];
        return [lastViewController df_topViewController];
    }
    
    UIViewController *presentedViewController = (UIViewController *)self.presentedViewController;
    return [presentedViewController df_topViewController];
}

#pragma mark - private

- (void)setNavBarAppearance:(UINavigationBar *)navigationBar {
    navigationBar.barTintColor = self.df_navigationBarTintColor;
    
    NSMutableDictionary *titleAttributes = [@{} mutableCopy];
    titleAttributes[NSForegroundColorAttributeName] = self.df_navigationBarForegourndColor;
    [navigationBar setTitleTextAttributes:titleAttributes];
    
    navigationBar.tintColor = self.df_navigationBarForegourndColor;
    navigationBar.backIndicatorImage = self.df_backIndicatorImage;
    navigationBar.backIndicatorTransitionMaskImage = self.df_backIndicatorImage;
}

@end
